<?php

App::uses('AppController', 'Controller');

/**
 * CommentsController Controller
 * 
 * @author oanhnn
 * @property Post $Post
 * @property Comment $Comment
 */
class CommentsController extends AppController {

    /**
     * Components
     * @var array
     */
    public $components = array();

    /**
     * Helpers
     * @var array
     */
    public $helpers = array();

    /**
     * Uses Models
     * @var array
     */
    public $uses = array('Post', 'Comment');

    /**
     * process before do action
     * @return void
     */
    public function beforeFilter() {
        parent::beforeFilter();
    }

    /**
     * process after action and before render view
     * @return void
     */
    public function beforeRender() {
        parent::beforeRender();
    }

    /**
     * add new comment
     * only working where request type ajax
     * @return void
     */
    public function add($post_id = null) {
        if ($this->request->is('ajax')) {
            $post = $this->Post->read(null, $post_id);
            if (!$post) {
                // error not found post
                $this->header('');
            } elseif (!empty($this->request->data)) {
                $data = $this->request->data;
                $data['Comment']['post_id'] = $post_id;
                $this->Comment->create();
                if ($this->Comment->save($data)) {
                    // created
                    $this->header('201');
                } else {
                    // not create
                    $this->header('');
                }
            } else {
                // no data
            }
        } else {
            // not ajax
            $this->response->statusCode('404');
        }
        $this->response->type('json');
        $this->render(null, null);
    }

    /**
     * delete comment
     * only working where request type ajax
     * @return void
     */
    public function delete($id = null) {
        
    }

}
